﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Common;
using HanGu.Entity;

namespace HanGu.CRM.Areas.CustomerGroup.Controllers
{
    public class DefaultController : AdminBaseController
    {

        public ActionResult Index()
        {
            return View(GetGroupList());
        }
        public ActionResult Selecter()
        {
            SearchParma.Pagination.PageSize = int.MaxValue;
            return View(GetGroupList());
        }
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            SearchParma.Pagination.RowCount = null;
            return View(GetGroupList());
        }

        List<HanGu.Entity.Group> GetGroupList()
        {
            List<HanGu.Entity.Group> GroupList = serviceGroup.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return GroupList;
        }

        public ActionResult Create(int id=0)
        {
            HanGu.Entity.Group group = new HanGu.Entity.Group();
            if (id > 0)
            {
                group = serviceGroup.GetByID(id);
                SearchParma.IntObj1 = id;
                var members=serviceGroup.GetSelected(SearchParma);
                members=members??new  List<VMemberGrouped>();
                TempData["SelectedMember"] = members.Select(c => c.MemberID).ToList<int>();
            }
            return View(group);
        }

        //
        // POST: /Group/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                HanGu.Entity.Group group = new HanGu.Entity.Group();
                TryUpdateModel(group, collection);
                string memberSelecteds = Request["MemberSelected"];
                string memberSelectings = Request["MemberSelecting"];
                string selectMemberIDs = string.Empty;
                if (!string.IsNullOrEmpty(memberSelecteds))
                {
                    if (!string.IsNullOrEmpty(memberSelectings))
                    {
                        selectMemberIDs = string.Format("{0},{1}", memberSelecteds, memberSelectings);
                    }
                    else
                    {
                        selectMemberIDs = memberSelecteds;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(memberSelectings))
                    {
                        selectMemberIDs = memberSelecteds;
                    }                    
                }
                if (!string.IsNullOrEmpty(selectMemberIDs))
                {
                    group.MemberID = selectMemberIDs;
                }

                if (group.ID > 0)
                {
                    serviceGroup.Update(group);
                }
                else
                {
                    serviceGroup.Add(group);
                }

                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }



        //
        // GET: /Group/Delete/5
        [HttpPost]
        public string Delete(int id, string guid)
        {
            try
            {
                serviceGroup.Delete(id);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";

        }
        public string DeleteSelected(int id, int guid)
        {
            try
            {
                serviceGroup.DeleteSelected(id, guid);
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";
        }
        public ActionResult MemberSelected(int id)
        {
            SearchParma.IntObj1 = id;
            string showAll = Request["showall"];
            if (!string.IsNullOrEmpty(showAll))
            {
                ViewData["pageSize"] = SearchParma.Pagination.PageSize = int.MaxValue;
            }
            //SearchParma.Lectcher = "1,2,3";
            var models = serviceGroup.GetSelected(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return PartialView( models);
        }
        public ActionResult MemberSelecting(int id)
        {
            SearchParma.IntObj1 = id;
            string showAll = Request["showall"];
            if (!string.IsNullOrEmpty(showAll))
            {
                ViewData["pageSize"] = SearchParma.Pagination.PageSize = int.MaxValue;
            }
            var models = serviceGroup.GetSelecting(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return PartialView( models);
        }
    }
}
